<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>

<div class="row">
	<div class="col-md-12">
		<div class="box box-primary">
    		<div class="box-header with-border">
    			<h3 class="box-title">
    			
    			</h3>
    			<div class="box-tools pull-right" id="adminDivTreeToolForm_${uuid}">
    				<shiro:hasPermission name="ADMINDIV_ADD">
			    	<a href="javascript:void(0)" class="btn btn-sm btn-success addBt ">
			            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加
			        </a>
			        </shiro:hasPermission>
			        
			        <shiro:hasPermission name="ADMINDIV_MOD">
			        <a href="javascript:void(0)" class="btn btn-sm btn-primary editBt ">
			            <span class="glyphicon glyphicon-edit" aria-hidden="true"></span> 修改
			        </a>
			        </shiro:hasPermission>
			        
			        <shiro:hasPermission name="ADMINDIV_DEL">
			        <a href="javascript:void(0)" class="btn btn-sm btn-danger delBt ">
			            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除
			        </a>
			        </shiro:hasPermission>
    			</div>
    		</div>
    		<div class="box-body">
    			<div id="adminDivTree_${uuid}" class="ztree">
		    	</div>
    		</div>
    	</div>
	</div>
</div>

<script type="text/javascript">
require(['wiseMen/module/AdminDivTree'],function(AdminDivTree){
	$(function () {
        var uuid = '${uuid}';
        var adminDivTree = $("#adminDivTree_"+uuid);
        var adminDivTreeToolForm = $("#adminDivTreeToolForm_"+uuid);
        
        var editAdminDivTree=function(params){
            params = params || {};
            var t = [];
            if (params.id) {
                t.push("id=" + params.id);
            }
            if (params.pid) {
                t.push("pid=" + params.pid);
            }
            
            var dialog=webUtils.dialog({
              	 title:'编辑行政区划',
              	 url:ctx + '/adminDivAct/editView.do?' + t.join("&"),
              	 onremove:function () {
                       if (params.onClose) {
                           params.onClose.call(null);
                       }
                   }
               });
            return dialog;
        }

        adminDivTreeToolForm.find(".addBt").click(function () {
            var nodes = adminDivTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
                var node = nodes[0];
                var sub = top.webUtils.topicSubscribe("adminDivTree.edit.success", function (args) {
                    var cnode = args.obj;
                    adminDivTree.addNodes(node, cnode);
                    dialog.close();
                });
                var dialog = editAdminDivTree({
                    pid: node.id,
                    onClose: function () {
                        sub.remove();
                    }
                });
            } else {
                webUtils.alert("请选择其中一个");
            }
        });

        adminDivTreeToolForm.find(".editBt").click(function () {
            var nodes = adminDivTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
                var node = nodes[0];
                var sub = top.webUtils.topicSubscribe("adminDivTree.edit.success", function (args) {
                    var cnode = args.obj;
                    adminDivTree.updateNode($.extend(node, cnode));
                    dialog.close();
                });
                var dialog = editAdminDivTree({
                    id:node.id,
                    onClose: function () {
                        sub.remove();
                    }
                });
            } else {
                webUtils.alert("请选择其中一个");
            }
        });

        adminDivTreeToolForm.find(".delBt").click(function () {
            var nodes = adminDivTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
            	webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var ids = [];
		                $.each(nodes, function (i, n) {
		                    ids.push(n.id);
		                });
		                $.post(ctx + "/adminDivAct/delAdminDiv.do", {ids: ids.join(",")}, function (data) {
		                    if (data.success) {
		                        $.each(nodes, function (i, n) {
		                        	adminDivTree.removeNode(n);
		                        });
		                    } else {
		                        webUtils.error(data.msg);
		                    }
		                }, 'json');
					}
        		});
            } else {
                alert("请勾选至少个");
            }
        });

        adminDivTree =new AdminDivTree({
        	target:adminDivTree,
        	seqAble:true
        });
    });
}); 
</script>